package com.kystar.muxplayer.manager;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.kystar.muxplayer.R;
import com.kystar.muxplayer.selfview.MyConstraintLayout;
import com.kystar.muxplayer.selfview.MyKeyBoard;
import com.kystar.muxplayer.ui.Collapse;
import com.kystar.muxplayer.ui.GroupView;
import com.kystar.muxplayer.ui.InputGroupView;
import com.kystar.muxplayer.ui.LayerGroupView;
import com.kystar.muxplayer.ui.OutputGroupView;
import com.kystar.muxplayer.ui.SystemGroupView;

/**
 *
 * Created by zhusheng on 2018-02-02.
 */

public class SettingsViewManager {
    private final Context context;
    private final ViewGroup rootView;
    private final OutputGroupView outputGroup;
    private final LayerGroupView layerGroup;
    private final InputGroupView inputGroup;
    private final SystemGroupView systemGroup;
    private Collapse lastGroup;
    MyConstraintLayout mConstraintLayout;
    MyKeyBoard mMyKeyBoard;

    public SettingsViewManager(ViewGroup root, MyConstraintLayout constraintLayout) {
        this.context = root.getContext();
        this.rootView = root;
        mConstraintLayout = constraintLayout;
        mMyKeyBoard = new MyKeyBoard(context,constraintLayout);
        this.outputGroup = new OutputGroupView(context,mMyKeyBoard);
        this.layerGroup = new LayerGroupView(context,mMyKeyBoard);
        this.inputGroup = new InputGroupView(context,mMyKeyBoard);
        this.systemGroup = new SystemGroupView(context,mMyKeyBoard);
        addGroup(outputGroup);
        addGroup(layerGroup);
        addGroup(inputGroup);
        addGroup(systemGroup);
    }

    private void addGroup(GroupView group) {
        rootView.addView(group.mGroupView, genLayoutParams(context));
        group.setOnTitleClick(v -> {
            if (lastGroup == group) {
                group.collapse(true);
                return;
            }
            if (lastGroup != null) {
                lastGroup.collapse(false);
            }
            group.collapse(true);
            lastGroup = group;
        });
    }

    @NonNull
    private static LinearLayout.LayoutParams genLayoutParams(Context context) {
        int space = context.getResources().getDimensionPixelSize(R.dimen.main_setting_group_space);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = space;
        params.gravity = Gravity.CENTER_HORIZONTAL;
        return params;
    }
}
